#!/usr/bin/python
# -*- coding:utf-8 -*-

import re
import os
import sys
import time
import urllib2
import optparse
import threading
import subprocess
import random
import smtplib
import traceback
import socket
from urlparse import urlparse
from multiprocessing import Process  #导入特定的成员
from Email import *  
from bs4 import BeautifulSoup, Tag
from lib2to3.fixer_util import String
# 如果你想在程序中用argv代表sys.argv，
# 则可使用：from sys import argv

# 跑BVT的参数
def para_proc():
    # 读取配置文件中的配置信息
    global URL_LIST  # URL列表
    global APK_PATH  # 产品apk的路径
    global PACKAGE_NAME  # 产品的包名
    global ACTIVITY_NAME  # 产品的Activity名称
    global TIME_LENGTH  # Monkey时长
    global SERVER  # 邮箱服务器
    global SENDER  # 发件人
    global RECEIVER  # 收件人
    global CC  # 抄送
    global BOX_SUBJECT  # 百度框的邮件标题
    global BROWSER_SUBJECT  # 百度浏览器的邮件标题
    global OTHER_SUBJECT  # 其他app的邮件标题
    global DAYSSTR  # 统计用户反馈天数
   
    config_file = open('BvtConfigfile', 'r')  # 以只读方式打开配置文件
    content = config_file.readlines()  # 逐行读取文件
   
    for index in range(0, len(content)):  # 逐行遍历配置文件内容
        if "UrlList" in content[index]:  # URL列表
            URL_LIST = content[index][8:-1]
        elif "ApkPath" in content[index]:  # 产品apk的路径
            APK_PATH = content[index][8:-1]
        elif "PackageName" in content[index]:  # 产品的包名
            PACKAGE_NAME = content[index][12:-1]
        elif "ActivityName" in content[index]:  # 产品Activity名称
            ACTIVITY_NAME = content[index][13:-1]
        elif "TimeLength" in content[index]:  # Monkey时长
            TIME_LENGTH = content[index][11:-1]
        elif "Server" in content[index]:  # 邮箱服务器
            SERVER = content[index][7:-1]
        elif "Sender" in content[index]:  # 发件人
            SENDER = content[index][7:-1]
        elif "Receiver" in content[index]:  # 收件人
            RECEIVER = content[index][9:-1]
        elif "Cc" in content[index]:  # 抄送
            CC = content[index][3:-1]
        elif "BoxSubject" in content[index]:  # 百度框的邮件标题
            BOX_SUBJECT = content[index][11:-1]
        elif "BrowserSubject" in content[index]:  # 百度浏览器的邮件标题
            BROWSER_SUBJECT = content[index][15:-1]
        elif "OtherSubject" in content[index]:  # 其他app的邮件标题
            OTHER_SUBJECT = content[index][13:-1]
        elif "DaysStr" in content[index]:  # 统计用户反馈天数
            DAYSSTR = content[index][8:-1]
    config_file.close()



# 主函数
def main():
    global time_stamp  # 时间戳
    time_stamp = time.strftime('%Y-%m-%d-%H-%M', time.localtime(time.time()))  # 时间格式转换
    yestoday_time_stamp = time.strftime('%Y年%m月%d日', time.localtime(time.time() - 24*60*60) )  #获取前一天数据
    start_time_stamp = time.strftime('%Y年%m月%d日', time.localtime(time.time() - int(DAYSSTR)*24*60*60) )
    # 4 发送BVT邮件
    print '4.Send Email......'
    if int(DAYSSTR) == 1: #只统计前一天数据
        daystr = '('+ yestoday_time_stamp +')'
    else:
        daystr = '('+ start_time_stamp + '-'+ yestoday_time_stamp +')'
    subject = BROWSER_SUBJECT + daystr
    #读取html文件
    fobj = open('template.html')
    data = fobj.read()
    fobj.close()
    soup = BeautifulSoup(data)
    html_title = soup.find(id='title')
    html_title.insert(0, "ufo-" + daystr)
   
    #表格内容插入
    percent_table = soup.find(id='percent-table')
    fp = open("seg_ufo_list.txt")
    index = 1
    while 1:
        line = fp.readline()
        if not line:
            break
        arr = line.split(' ');
        tr_tag = soup.new_tag('tr')
        td_tag_1 = soup.new_tag('td')
        td_tag_1.insert(0, str(index))
        td_tag_2 = soup.new_tag('td')
        td_tag_2.insert(0, arr[0])
        td_tag_3 = soup.new_tag('td')
        td_tag_3.insert(0, arr[1])
        td_tag_4 = soup.new_tag('td')
        link_tag = soup.new_tag('a', href='http://www.example.com')
        link_tag.insert(0, '点击下载')
        td_tag_4.append(link_tag)
        tr_tag.append(td_tag_1)
        tr_tag.append(td_tag_2)
        tr_tag.append(td_tag_3)
        tr_tag.append(td_tag_4)
        percent_table.append(tr_tag)
        index = index + 1
        pass
    fp.close()
   
    #保存html文件
    #fobj = file("email.html", "w")
    #fobj.write(str(soup))
    #fobj.close()
    
    message = str(soup)
    files = []  # 附件，错误log压缩文件不存在或大于10M则不带附件

    email = Email()
    email.sendHtmlAndAttachment(eval(SERVER), SENDER, RECEIVER.split(', '), CC.split(', '), subject, str(soup), files)
    print '4.Done!!!!!!'



# 脚本程序入口
if __name__ == "__main__":
    para_proc()  # 一些参数
    main()  # 主函数
    os._exit(0)  # 退出脚本
